//
//  MainViewController.swift
//  新浪微博
//
//  Created by 徐绩 on 16/4/15.
//  Copyright © 2016年 ayumi. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let composeBtnClickClosure = {

            let composeView = XJComposeView()
//            self.view.addSubview(composeView)
            composeView.show(self)
        }
        
        let tabBar = XJTabBar()
        
        tabBar.composeBtnClickClosure = composeBtnClickClosure
        
        self.setValue(tabBar, forKey: "tabBar")
        
        addChildViewControllers()
    }
}

// MARK: - 添加控制器
extension MainViewController {
    
    //添加所有子控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeViewController(), title: "主页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    private func addChildViewController(vc: UIViewController ,title: String, imageName: String) {
        
        vc.title = title

        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        
        let nav = UINavigationController(rootViewController: vc)
        
        //添加导航控制器
        addChildViewController(nav)
    }
    
}